#pragma once
#include<iostream>
#include<string>
#include<fstream>
#include<unordered_map>
#include<unistd.h>
#include"Log.hpp"

using namespace log_ns;
using namespace std;
const static string sep =  ": ";

class Dict{
private:
    void LoadDict(const string& path)
    {
        ifstream in(path);
        if(!in.is_open())
        {
            LOG(FATAL,"open %s failed\n",path.c_str());
            exit(1);
        }
        string line;//表示每一行
        while(getline(in,line))
        {
            if(line.empty())
            {
                continue;
            }
            auto pos = line.find(sep);
            if(pos==string::npos)
            {
                continue;
            }
            string key = line.substr(0,pos);
            if (key.empty())
                continue;
            string value = line.substr(pos+sep.size());
            if (value.empty())
                continue;
            _dict.insert(make_pair(key,value));
        }
        LOG(INFO, "load %s done\n", path.c_str());
        in.close();
    }
public:
    Dict(const string& dict_path):_dict_path(dict_path)
    {
        LoadDict(_dict_path);
    }
    string Translate(string word)
    {
        if(word.empty())
        {
            return "None";
        }
        auto iter = _dict.find(word);
        if(iter==_dict.end())
        {
            return "None";
        }
        else return iter->second;
    }
    ~Dict()
    {

    }
private:
    unordered_map <string,string> _dict;
    string _dict_path;
};